﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm.Easy_2073_TimeRequiredToBuy
{
    class MainProject
    {
        static void Main(string[] args)
        {
            Console.WriteLine(TimeRequiredToBuy(new int[] { 84, 49, 5, 24, 70, 77, 87, 8}, 3));
            Console.ReadKey();
        }
        public static int TimeRequiredToBuy(int[] tickets, int k)
        {
            //模拟+队列
            Queue<int> que = new Queue<int>(tickets);
            int second = 0;
            bool isOver = false;
            while (!isOver)
            {
                int size = que.Count;
                for (int i = 0; i < size; i++)
                {
                    int q = que.Dequeue();
                    if (q == 0) { que.Enqueue(q); continue; }
                    second++;
                    q--;
                    que.Enqueue(q);
                    if (i == k && q == 0) { isOver = true;break; }
                }
                if (isOver) break;
            }
            return second;
        }
    }
}
